package jdbc;

import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class StudentAdd {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        Connection connection = DBUtil.getConnection();

        PreparedStatement ps = null;
        try {

            System.out.println("链接已经建立");
            System.out.println("请输入学生姓名:");
            String sname = in.next();
            System.out.println("请输入学生性别:");
            String sex = in.next();
            System.out.println("请输入手机号:");
            String telephone = in.next();
            System.out.println("请输入qq号:");
            String qq = in.next();
            System.out.println("请输入微信号:");
            String wechat = in.next();
            System.out.println("请输入年级id:");
            Long gradeid = in.nextLong();
            System.out.println("请输入加入班级时间:yyyy/mm/dd");
            String S = in.next();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//可以方便地修改日期格式
            Date enterdate = dateFormat.parse(S);
            System.out.println("请输入来自的城市:");
            String fromcity = in.next();
            System.out.println("请输入生日:yyyy-mm-dd");
            String s = in.next();
            Date birthday = dateFormat.parse(s);
            System.out.println("请输入密码:");
            String pass = in.next();
            String sql = "INSERT INTO s_student(sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,birthday,pass) VALUES(?,?,?,?,?,?,?,?,?,?);";
            ps = connection.prepareStatement(sql);
            System.out.println(sql);
            ps.setString(1, sname);
            ps.setString(2, sex);
            ps.setString(3, telephone);
            ps.setString(4, qq);
            ps.setString(5, wechat);
            ps.setLong(6, gradeid);
            ps.setDate(7, new java.sql.Date(enterdate.getTime()));
            ps.setString(8, fromcity);
            ps.setObject(9, birthday);
            ps.setString(10, pass);
            int num = ps.executeUpdate();

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                ps.close();
                connection.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }
}
